package com.platform.task.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.xxl.job.core.executor.XxlJobExecutor;

/**
 * xxl-job config
 *
 * @author xuxueli 2017-04-28
 */
@Configuration
public class TaskJobConfig {
	private Logger logger = LoggerFactory.getLogger(TaskJobConfig.class);

	@Value("${spring.platform.task.job.admin.addresses}")
	private String addresses;

	@Value("${spring.platform.task.job.executor.appname}")
	private String appname;

	@Value("${spring.platform.task.job.executor.ip}")
	private String ip;

	@Value("${spring.platform.task.job.executor.port}")
	private int port;

	@Value("${spring.platform.task.job.executor.logpath}")
	private String logpath;

	@Value("${spring.platform.task.job.accessToken}")
	private String accessToken;

	@Value("${spring.platform.task.job.enable}")
	private boolean enable;

	@Bean(initMethod = "start", destroyMethod = "destroy")
	public XxlJobExecutor xxlJobExecutor() {
		logger.info(">>>>>>>>>>> task-job config enable:"+enable);
		if (enable) {
			logger.info(">>>>>>>>>>> task-job config init.");
			XxlJobExecutor xxlJobExecutor = new XxlJobExecutor();
			xxlJobExecutor.setIp(ip);
			xxlJobExecutor.setPort(port);
			xxlJobExecutor.setAppName(appname);
			xxlJobExecutor.setAdminAddresses(addresses);
			xxlJobExecutor.setLogPath(logpath);
			xxlJobExecutor.setAccessToken(accessToken);
			return xxlJobExecutor;
		}
		return null;
	}

}